package com.devcon5.StudentPlanner;

import java.util.Calendar;

import baseclasses.Course;
import database.CourseDAO;
import forms.CourseForm;

import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;

public class AddCourse extends CourseForm {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		//if returning from orientation change or activity switch
		if(savedInstanceState==null)
		{
			//initialize weekday booleans
			weekDayBooleans = new boolean[]{false, false, false, false, false, false, false};
			
			//initialize calendar
			startTimeCalendar = Calendar.getInstance();
			//initiate course time to 12 PM for convenience
			startTimeCalendar.set(Calendar.HOUR_OF_DAY, 12);
			startTimeCalendar.set(Calendar.MINUTE, 0);
			
			endTimeCalendar = Calendar.getInstance();
			//initiate course end time to 12:50 PM for convenience
			endTimeCalendar.set(Calendar.HOUR_OF_DAY, 12);
			endTimeCalendar.set(Calendar.MINUTE, 50);
		}
		//set the display to the course's data
		courseTimeText.setText(Course.getStartTimeString(startTimeCalendar, "hh:mm a"));
		courseEndTimeText.setText(Course.getStartTimeString(endTimeCalendar, "hh:mm a"));
		courseDaysText.setText(Course.getWeekDays(weekDayBooleans));

	}

	@Override
	protected void onSaveButtonClicked() {

		String courseName = courseNameEditText.getText().toString();
		String location = courseLocationEditText.getText().toString();
		int credits;
		try{	//parse the credits to an int since we're using them to calculate grade points
			credits = Integer.parseInt(courseCreditsEditText.getText().toString());
		}
		catch(java.lang.NumberFormatException e){
			credits = 3;
		}
		//if course name is valid (not blank) proceed
		if(courseNameValid(courseName)){


			//if weekdays are valid (at least one checked) proceed
			if(weekDaysValid(weekDayBooleans)){

				//if all input is valid create course object and add to database
				Course newCourse = new Course(courseName, startTimeCalendar, weekDayBooleans, credits, endTimeCalendar, location);

				if(CourseDAO.addCourse(context, newCourse)==-1){	//if there is an error (unedited flag still set), display a toast
					Toast.makeText(getBaseContext(), "Error adding course", Toast.LENGTH_LONG).show();
				}
				else{
				
					onBackPressed();
					finish();
				}
			}

			//if weekday is not valid (nothing selected) show message
			else{
				Toast.makeText(getBaseContext(), "You must select atleast 1 weekday!", Toast.LENGTH_LONG).show();
			}
		}
		//if course name is not valid show message
		else{
			Toast.makeText(getBaseContext(),  "Course title cannot be blank!", Toast.LENGTH_LONG).show();
		}

	}
	
	@Override
	protected void onCourseTimeLayoutClicked(View v){	//when the time layout is clicked
		
		v.setBackgroundResource(android.R.drawable.list_selector_background);
		new TimePickerDialog(context, new OnTimeSetListener(){	//display the time picker and get the values
		
			public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
				startTimeCalendar.set(Calendar.HOUR_OF_DAY, arg1);
				startTimeCalendar.set(Calendar.MINUTE, arg2);
				courseTimeText.setText(Course.getStartTimeString(startTimeCalendar, "hh:mm a"));
				
				if( !endTimeSelected )
				{	//set the end time to the start time plus 50 minutes
					long time = startTimeCalendar.getTimeInMillis() + (1000*50*60);
					endTimeCalendar.setTimeInMillis( time );
					courseEndTimeText.setText(Course.getEndTimeString(endTimeCalendar, "hh:mm a"));
				}
			}
		}, startTimeCalendar.get(Calendar.HOUR_OF_DAY), startTimeCalendar.get(Calendar.MINUTE), false).show();
		
	}
	
	@Override
	protected void onCourseEndTimeLayoutClicked(View v){
		
		v.setBackgroundResource(android.R.drawable.list_selector_background);
		new TimePickerDialog(context, new OnTimeSetListener(){
	
			public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
				endTimeCalendar.set(Calendar.HOUR_OF_DAY, arg1);
				endTimeCalendar.set(Calendar.MINUTE, arg2);
				courseEndTimeText.setText(Course.getEndTimeString(endTimeCalendar, "hh:mm a"));
				
				endTimeSelected = true;	//Set end time selected = true so the start time method won't set the end time to startTime + 50 minutes again
			}
		}, endTimeCalendar.get(Calendar.HOUR_OF_DAY), endTimeCalendar.get(Calendar.MINUTE), false).show();
		
	}

}